<?php
session_start();
include("conexion.php");
$lugar=$_GET['lugar'];
$nombre=$_SESSION['nombre'];
mysql_select_db($dbname, $con);
$sql="SELECT id FROM usuarios WHERE nick='$nombre'";
$resultado=mysql_query($sql, $con);
While ($row=mysql_fetch_array($resultado)){
   $id=$row['id'];
} 


$sql="SELECT * FROM correo WHERE id_usuario='$id'";
$resultado=mysql_query($sql, $con);
While ($row=mysql_fetch_array($resultado)){
   $email=$row['email'];
   $pwd=$row['password'];
} 
/* connect to gmail */
$hostname = '{imap.gmail.com:993/imap/ssl}INBOX';
$username = $email;
$password = $pwd;

/* try to connect */
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());

/* grab emails */
$emails = imap_search($inbox,'ALL');

/* if emails are returned, cycle through each... */
if($emails) {
  /* begin output var */
  $output = '';
  $count=0;
  
  /* put the newest emails on top */
  rsort($emails);
  /* for every email... */
  
  foreach($emails as $email_number) {
	 
	$count++;
    /* get information specific to this email */
    $overview = imap_fetch_overview($inbox,$email_number,0);
    $message = imap_fetchbody($inbox,$email_number,2);
    
    /* output the email header information */
	
	    
    /* output the email body */
	if($lugar==$email_number){
	$output.= '<div class="mensaje_email" style="display:">';
    $output.= '<div class="body">'.$message.'</div>';
	$output.='</div>';
	

	break;
	}
	}
	
	
	
	echo $output;
} 

/* close the connection */
imap_close($inbox);
?>
